import { reactive } from 'vue'
import { addRole, updateRole, getRolePageList, getRoleForm, getMenuListByRole } from '@/api/system/role'
import { getRoleMenuOptionList, getRoleDataPermissionOptions } from '@/api/system/menu'

export function useDetail(getFormValues: Function) {
  const status = reactive({
    saving: false,
    loading: false
  })

  const createOrUpdate = async () => {
    const values = getFormValues()
    if (status.saving) return false
    status.saving = true
    try {
      values?.code ? await updateRole(values.code, values) : await addRole(values)
      status.saving = false
      return true
    } catch (err) {
      status.saving = false
      return false
    }
  }

  const getRoleList = async (data: any) => {
    return getRolePageList(data)
  }

  const getRoleById = async (code: any) => {
    return getRoleForm(code)
  }

  const getMenuListByRoleById = async (code: any) => {
    return getMenuListByRole(code)
  }

  const getMenuList = async () => {
    return getRoleMenuOptionList()
  }

  const getDataPermissionOptions = async () => {
    return getRoleDataPermissionOptions()
  }

  return {
    status,
    createOrUpdate,
    getRoleById,
    getRoleList,
    getMenuList,
    getMenuListByRoleById,
    getDataPermissionOptions
  }
}
